<?php
/**
* Scans images on the server location and creates an album for those images.
* Operated manually
* Looks like @scanner class body
*/
class scanner_manual
{
	public $album_id=0;
	
	private $album_folder='/tmp';
	private $album_name='';
	
	private $error='';
	private $BR="\r\n<br />";
	private $subdomain_id=0;
	
	private $db;
	
	public function __construct($album_folder='/tmp', $album_id=0)
	{
		global $subdomain_id;
		$this->subdomain_id = (int)$subdomain_id;
		$this->db = new mysql();
		
		$album_folder = $this->valid_name($album_folder);
		if(!is_dir($album_folder))
		{
			die('Not an album folder: '.$album_folder);
		}
		else
		{
			$this->album_folder = $album_folder;
			chdir($album_folder);
		}
		
		$this->album_id = (int)$album_id;
		$this->register_images();
	}

	
	/**
	* Save all album images into the database
	*/
	private function register_images()
	{
		#chdir($this->album_folder);
		$images = array_merge(glob('*.jpg', GLOB_NOSORT), glob('*.JPG', GLOB_NOSORT));
		#print_r($images); die();
		if(!count($images))
		{
			$this->error = 'Cannot find images. Check extension, case sensitivity, ...';
		}
		
		#print_r($images); #die();
		foreach($images as $i => $image)
		{
			$this->register_image($image);
		}
		
		# Update images counter
		#$counter = count($images);
		#$update_sql="UPDATE gallery_images SET images_counter=images_counter+{$counter} WHERE album_id={$this->album_id};";
	}
	
	/**
	* Register an individual image into the database
	*/
	private function register_image($image_file='default.jpg')
	{
		$category_id = DEFAULT_CATEGORY_ID; # 0; # Unknown by default
		#chdir($this->album_folder);
		
		# INSERT INTO ...
		# save other info, exif, dimensions, colors, sizes, ...
		# create thumbs (resize), small pic, medium pic, large pic
		# move original pic to its own folder - for original file download purpose
		#$image_id = mt_rand(1000, 9999);
		
		# Helpful when the image filename contains invalid characters
		$image_file = addslashes($this->valid_name($image_file));
		
		$full_path = "{$this->album_folder}/{$image_file}";
		#die($full_path);
		if(!is_file($full_path)) return null;
		
		$filesize_bytes = filesize($full_path);
		$image_code = tools::unique_code();
		$image_hash = sha1_file($full_path);
		
		$info = array();
		$image_sizes = getimagesize($full_path, $info);
		$dimension_width = isset($image_sizes[0])?(int)$image_sizes[0]:0;
		$dimension_height = isset($image_sizes[1])?(int)$image_sizes[1]:0;
		
		$created_on = filemtime($full_path);
		if(!$created_on) $created_on=0;
		
		$image_caption = addslashes(
			implode(' ',
				preg_split('/[^a-z0-9]+/is', preg_replace('/\.(jpg|jpeg|gif|png)$/is', '', $image_file))
			)
		);
		
		$insert_image_sql="
INSERT IGNORE INTO gallery_images (
	subdomain_id, added_on, is_active, is_approved,
	created_on,
	dimension_width, dimension_height, filesize_bytes,
	image_code,
	image_name, image_caption, image_keywords, image_hash,
	image_description
) VALUES (
	{$this->subdomain_id}, UNIX_TIMESTAMP(CURRENT_TIMESTAMP()), 'Y', 'Y',
	{$created_on},
	{$dimension_width}, {$dimension_height}, {$filesize_bytes},
	'{$image_code}',
	'{$image_file}', '{$image_caption}', '', '{$image_hash}',
	''
);";
		#die($insert_image_sql);
		$this->db->query($insert_image_sql);
		if($image_id = $this->db->insert_id())
		{
			# Add image sizes
			# Add as default image size for later modifications
			$size_id = THUMBNAIL_ORIGINAL_SIZE_ID;
			$image_mime = exif_imagetype($full_path);
			$image_exifinfo = ''; # addslashes(serialize(exif_read_data($full_path)));
			#print_r(exif_read_data($full_path)); die();

			$automatic_scanned_size_sql="
INSERT IGNORE INTO gallery_images_sizes (
	subdomain_id, added_on, is_active,
	image_id, size_id, image_filesize,
	image_name, image_name_local, image_hash,
	image_mime, image_exifinfo
) VALUES (
	{$this->subdomain_id}, UNIX_TIMESTAMP(CURRENT_TIMESTAMP()), 'Y',
	{$image_id}, {$size_id}, {$filesize_bytes},
	'{$image_file}', '', '{$image_hash}',
	'{$image_mime}', '{$image_exifinfo}'
);";
			#die($automatic_scanned_size_sql);
			$this->db->query($automatic_scanned_size_sql);
			
			
			# Also, assign the image to the album
			$album_assign_sql="INSERT INTO gallery_images_albums (subdomain_id, added_on, is_active, is_approved, image_id, album_id) VALUES ({$this->subdomain_id}, UNIX_TIMESTAMP(CURRENT_TIMESTAMP()), 'Y', 'Y', {$image_id}, {$this->album_id});";
			$this->db->query($album_assign_sql);
			
			# Mark as unknown category
			$album_assign_sql="INSERT INTO gallery_images_categories (subdomain_id, added_on, is_active, is_approved, image_id, category_id) VALUES ({$this->subdomain_id}, UNIX_TIMESTAMP(CURRENT_TIMESTAMP()), 'Y', 'Y', {$image_id}, {$category_id});";
			$this->db->query($album_assign_sql);
		}

		echo "{$this->BR}Registering: {$image_file} - Image ID: {$image_id}, Album ID: {$this->album_id}, Category ID: {$category_id}";

		return $image_id;
	}
	
	
	/**
	* Valid name
	*/
	private function valid_name($image_path_name='')
	{
		$image_path_name = str_replace('\\', '/', $image_path_name);
		$image_path_name = preg_replace('/[^a-z0-9\_\-\.\ \/\:]+/is', '', $image_path_name);
		
		return $image_path_name;

	}
}
?>